<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"sekla.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="汇编浮点数原理与实现一组对比:">
<meta property="og:type" content="article">
<meta property="og:title" content="汇编浮点数原理与实现">
<meta property="og:url" content="http://sekla.cn/2021/09/06/%E6%B1%87%E7%BC%96%E4%B8%AD%E7%9A%84%E6%B5%AE%E7%82%B9/index.html">
<meta property="og:site_name" content="Sekla&#39;s Blog">
<meta property="og:description" content="汇编浮点数原理与实现一组对比:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/09/17/vETMha4kpcKgIQV.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/GmvkSUr8PbOf6xs.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/ClOiRWTF7kZ65Gn.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/BQzfWRIaiYd8tEK.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/g2wpsAJbEYzmyMW.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/YOFzj4lMnSBWg9d.png">
<meta property="og:image" content="https://i.loli.net/2021/09/17/oqL8ysYuCQErzPj.png">
<meta property="article:published_time" content="2021-09-06T03:00:00.000Z">
<meta property="article:modified_time" content="2021-10-18T14:47:22.989Z">
<meta property="article:author" content="Sekla">
<meta property="article:tag" content="汇编">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/09/17/vETMha4kpcKgIQV.png">

<link rel="canonical" href="http://sekla.cn/2021/09/06/%E6%B1%87%E7%BC%96%E4%B8%AD%E7%9A%84%E6%B5%AE%E7%82%B9/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>汇编浮点数原理与实现 | Sekla's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script type="text/javascript" src="/js/my_js/clicklove.js"></script>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Sekla's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Keep Learning Keep Doing</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">100</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://sekla.cn/2021/09/06/%E6%B1%87%E7%BC%96%E4%B8%AD%E7%9A%84%E6%B5%AE%E7%82%B9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Sekla">
      <meta itemprop="description" content="Sekla">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sekla's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          汇编浮点数原理与实现
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-06 11:00:00" itemprop="dateCreated datePublished" datetime="2021-09-06T11:00:00+08:00">2021-09-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-18 22:47:22" itemprop="dateModified" datetime="2021-10-18T22:47:22+08:00">2021-10-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">计算机系统</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="汇编浮点数原理与实现"><a href="#汇编浮点数原理与实现" class="headerlink" title="汇编浮点数原理与实现"></a>汇编浮点数原理与实现</h1><p><strong>一组对比</strong>:</p>
<p><img src="https://i.loli.net/2021/09/17/vETMha4kpcKgIQV.png" alt="image-20210917114030710"></p>
<p><img src="https://i.loli.net/2021/09/17/GmvkSUr8PbOf6xs.png" alt="image-20210917114035610"></p>
<a id="more"></a>

<p><img src="https://i.loli.net/2021/09/17/ClOiRWTF7kZ65Gn.png" alt="image-20210917114038925"></p>
<h2 id="发现不同"><a href="#发现不同" class="headerlink" title="发现不同"></a>发现不同</h2><p>Float和double类型相加不同并且多出来的有FLD，FADD，FSTP等指令他们是什么意思呢？</p>
<h3 id="浮点数的存储"><a href="#浮点数的存储" class="headerlink" title="浮点数的存储"></a>浮点数的存储</h3><p>浮点数的运算完全不同于整数，从寄存器到指令，都有一套独特的处理流程，浮点单元也称作x87 FPU</p>
<p>单浮点数格式如：|1符号位|8指数位|23位尾数|存储</p>
<p>IEEE浮点数标准：</p>
<table>
<thead>
<tr>
<th><strong>格式</strong></th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td>单精度</td>
<td>32位：符号占1位，指数占8位，尾数中的小数部分占23位</td>
</tr>
<tr>
<td>双精度</td>
<td>64位：符号占1位，指数占11位，尾数中的小数部分占52位</td>
</tr>
<tr>
<td>扩展精度</td>
<td>80位：符号占1位，指数占16位，尾数中的小数部分占63位</td>
</tr>
</tbody></table>
<h3 id="一个例子"><a href="#一个例子" class="headerlink" title="一个例子"></a>一个例子</h3><p>关于指数，由于需要表示正负两种数据，IEEE标准规定单精度指数以127为分割线，实际存储的数据是指数加127所得结果，127为高位为零，后7位为1所得，其他双精度也以此方式计算。</p>
<p><strong>例如定义</strong></p>
<p><code>Float x=12.34;</code></p>
<p>可以看到汇编文件中显示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">x:</span><br><span class="line">    .long  1095069860</span><br></pre></td></tr></table></figure>

<p>gcc编译器将变量定义成了<strong>long类型</strong>。不过没关系，因为都是四个字节的数字，将1095069860转换为二进制数为：</p>
<p>0 10000010 10001010111000010100100</p>
<p>根据划分规则，符号位为0为正数，指数为130，减去127得3，尾数加上**1.**，形式化为1. 10001010111000010100100，扩大2^23次方为110001010111000010100100，十进制为12939428，后除以2^(23-3)结果为12.3400001525····与我们所定义的数相符合。</p>
<h2 id="浮点数的存储运算"><a href="#浮点数的存储运算" class="headerlink" title="浮点数的存储运算"></a>浮点数的存储运算</h2><p><strong>浮点数的浮点运算单元：FPU。</strong></p>
<p>在大多数现代计算机的架构中，一些浮点数运算跟整数运算是分开的。这些分别在不同的架构上差别很大。有一些架构，例如Intel的x86处理器设计了浮点数寄存器，另一些架构中，处理浮点数甚至有独立的时频时域。</p>
<p>FPU一般有8个独立寻址的80bit寄存器，一般写作R0,R1····R7.</p>
<p>他们一般以堆栈形式来组织，统称为寄存器栈，一般栈顶写作st(0)，最后一个寄存器写作st(7)。</p>
<p>FPU另有3个16位的寄存器，分别为控制寄存器、状态寄存器、标记寄存器</p>
<h2 id="状态寄存器"><a href="#状态寄存器" class="headerlink" title="状态寄存器"></a>状态寄存器</h2><table>
<thead>
<tr>
<th><strong>位</strong></th>
<th><strong>含义</strong></th>
<th><strong>位</strong></th>
<th><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td>0</td>
<td>非法操作异常</td>
<td>1</td>
<td>非规格化操作数异常</td>
</tr>
<tr>
<td>2</td>
<td>除数为0异常</td>
<td>3</td>
<td>溢出标志异常</td>
</tr>
<tr>
<td>4</td>
<td>下溢标志异常</td>
<td>5</td>
<td>精度异常标志</td>
</tr>
<tr>
<td>6</td>
<td>堆栈错误</td>
<td>7</td>
<td>错误汇总状态</td>
</tr>
<tr>
<td>8</td>
<td>条件代码位c0</td>
<td>9</td>
<td>条件代码位c1</td>
</tr>
<tr>
<td>10</td>
<td>条件代码位c2</td>
<td>11-13</td>
<td>堆栈顶指针</td>
</tr>
<tr>
<td>14</td>
<td>条件代码位c3</td>
<td>15</td>
<td>繁忙标志</td>
</tr>
</tbody></table>
<p>记录浮点计算过程中的状态。其中读取状态寄存器内容可以使用fstsw %aw指令</p>
<h2 id="控制寄存器与标志寄存器"><a href="#控制寄存器与标志寄存器" class="headerlink" title="控制寄存器与标志寄存器"></a>控制寄存器与标志寄存器</h2><p>控制寄存器位含义：</p>
<p>0 —— 非法操作异常掩码 </p>
<p>1 —— 非法格式化异常掩码 </p>
<p>2 —— 除数为0异常掩码</p>
<p>3 —— 溢出异常掩码 </p>
<p>4 —— 下溢异常掩码 </p>
<p>5 —— 精度异常亚曼 </p>
<p>6-7 —— 保留 </p>
<p>8-9 —— 精度控制（00单精度，01未使用，10双精度，11扩展精度） </p>
<p>10-11 —— 舍入控制（00舍入到最近，01向下舍入，10向上舍入，11向0舍入） </p>
<p>12 —— 无穷大控制 </p>
<p>13–15 —— 保留</p>
<p><strong>一般读取控制寄存器指令为</strong>:</p>
<table>
<thead>
<tr>
<th>fstcw control</th>
<th>加载到内存</th>
</tr>
</thead>
<tbody><tr>
<td>fldcw control</td>
<td>加载到控制器</td>
</tr>
<tr>
<td>finit</td>
<td>初始化FPU，但不会改变FPU数据</td>
</tr>
<tr>
<td>fnclex</td>
<td>清空浮点异常</td>
</tr>
<tr>
<td>fssave</td>
<td>FPU状态保存</td>
</tr>
<tr>
<td>fldenv buffer</td>
<td>恢复保存环境</td>
</tr>
<tr>
<td>fstenv</td>
<td>保存控制寄存器、状态寄存器、标记寄存器、FPU指令指针偏移量、FPU数据指针，FPU最后执行的操作码到内存中。</td>
</tr>
</tbody></table>
<p>标志寄存器，分别0-15位分别标志r0-r7共8个寄存器，每个寄存器占2位。</p>
<p>2位含义一般为<br>11 —— 合法扩展精度</p>
<p>01 —— 零 </p>
<p>10 —— 特殊浮点 </p>
<p>11 —— 无内容</p>
<h2 id="浮点数计算指令"><a href="#浮点数计算指令" class="headerlink" title="浮点数计算指令"></a>浮点数计算指令</h2><table>
<thead>
<tr>
<th>flds  value</th>
<th>加载内存中的单精浮点到fpu寄存器堆栈</th>
</tr>
</thead>
<tbody><tr>
<td>fldl  value</td>
<td>加载内存中的双精浮点到fpu寄存器堆栈</td>
</tr>
<tr>
<td>fldt  value</td>
<td>加载内存中的扩展精度点到fpu寄存器堆栈</td>
</tr>
<tr>
<td>fld  %st(i)</td>
<td>将%st(i)寄存器数据压入fpu寄存器堆栈</td>
</tr>
<tr>
<td>fsts  value</td>
<td>单精度数据保存到value，不出栈</td>
</tr>
<tr>
<td>fstl  value</td>
<td>双精度数据保存到value，不出栈</td>
</tr>
<tr>
<td>fstt  value</td>
<td>扩展精度数据保存到value，不出栈</td>
</tr>
<tr>
<td>fstps  value</td>
<td>单精度数据保存到value，出栈</td>
</tr>
<tr>
<td>fstpl  value</td>
<td>双精度数据保存到value，出栈</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>fstpt  value</th>
<th>扩展精度数据保存到value，出栈</th>
</tr>
</thead>
<tbody><tr>
<td>fxch  %st(i)</td>
<td>交换%st(0)和%st(i)</td>
</tr>
<tr>
<td>fld1</td>
<td>把 +1.0 压入 FPU 堆栈中</td>
</tr>
<tr>
<td>fldl2t</td>
<td>把 10 的对数(底数2)压入 FPU 堆栈中</td>
</tr>
<tr>
<td>fldl2e</td>
<td>把 e 的对数(底数2)压入 FPU 堆栈中</td>
</tr>
<tr>
<td>fldpi</td>
<td>把 pi 的值压入 FPU 堆栈中</td>
</tr>
<tr>
<td>fldlg2</td>
<td>把 2 的对数(底数10)压入 FPU 堆栈中</td>
</tr>
<tr>
<td>fldln2</td>
<td>把 2 的对数(底数e) 压入堆栈中</td>
</tr>
<tr>
<td>fldz</td>
<td>把 +0.0 压入压入堆栈中</td>
</tr>
</tbody></table>
<h2 id="简单的分辨技巧"><a href="#简单的分辨技巧" class="headerlink" title="简单的分辨技巧"></a>简单的分辨技巧</h2><p>上一页虽然列举的指令很多，但是我们可以看出来前缀f一般代表对FPU操作，LD加载，ST保存设置，P后缀弹出栈。</p>
<h2 id="具体的运算操作"><a href="#具体的运算操作" class="headerlink" title="具体的运算操作"></a>具体的运算操作</h2><p>浮点运算指令与整型运算指令差不多，大部分只是在前面加了一个前缀-f</p>
<p><strong>例如：</strong></p>
<p>add – fadd 浮点加法 div - fdiv 浮点除法。</p>
<p><strong>特殊：</strong></p>
<p>fdivr –反向浮点除法 fsubr-反向浮点减法。</p>
<p>接下来就可以按照运算操作进行编写指令运算。</p>
<h2 id="其他的指令"><a href="#其他的指令" class="headerlink" title="其他的指令"></a>其他的指令</h2><p>不光进行简单的加减乘除，x87还提供了一些高级运算。</p>
<p>例如余弦正弦</p>
<table>
<thead>
<tr>
<th>fabs</th>
<th>计算st0中的绝对值</th>
</tr>
</thead>
<tbody><tr>
<td>fchs</td>
<td>改变st0中的值的符号</td>
</tr>
<tr>
<td>fcos</td>
<td>计算st0中的值的余弦</td>
</tr>
</tbody></table>
<p>等等很多其他高级运算指令。</p>
<p>还有专门对浮点数进行比较的指令，如：</p>
<table>
<thead>
<tr>
<th>fcom</th>
<th>比较st0和st1寄存器的值</th>
</tr>
</thead>
<tbody><tr>
<td>fcom  %st(x)</td>
<td>比较st0和stx寄存器的值</td>
</tr>
</tbody></table>
<p>等等····</p>
<p>浮点数比较的结果放入状态寄存器的c0，c2，c3条件代码位中。</p>
<p>还有一些组合指令能够快速比较大小并判断条件代码位。</p>
<h1 id="新的不同"><a href="#新的不同" class="headerlink" title="新的不同"></a>新的不同</h1><p><img src="https://i.loli.net/2021/09/17/BQzfWRIaiYd8tEK.png" alt="image-20210917115524598"></p>
<p><img src="https://i.loli.net/2021/09/17/g2wpsAJbEYzmyMW.png" alt="image-20210917115527379"></p>
<h2 id="为什么不是原来的指令？"><a href="#为什么不是原来的指令？" class="headerlink" title="为什么不是原来的指令？"></a>为什么不是原来的指令？</h2><p><strong>SSE</strong> – Streaming SIMD Extension，是Intel从PIII开始加入的一种x86扩展指令集。在SSE以前，x86的浮点运算都是以栈式FPU完成的。而SSE一方面让浮点运算可以像整数运算的模式、如 add eax , ebx 那样通过直接访问寄存器完成，绕开了讨厌的栈，另一方面引入了SIMD这个概念。SIMD – Single Instruction Multiply Data，顾名思义，它可以同时让一条指令在多个数据上执行，这种体系结构在一度在大型机上非常流行，需要经常进行海量运算的大型机器通常会通过一个数学SIMD虚拟机加快处理速度，比如同时让一组数据执行一个变换，数据的规模有上百万之巨，而SIMD则可以优化数据的存储与运算，减免某些切换Context的开销。</p>
<p>SSE指令集提供了70条新指令。</p>
<h3 id="关于SSE"><a href="#关于SSE" class="headerlink" title="关于SSE"></a>关于SSE</h3><p>SSE加入新的<strong>8个128位寄存器（XMM0～XMM7）</strong>。而AMD发表的x86-64延伸架构（又称AMD64）再加入额外8个寄存器。除此之外还有一个新的32位的控制／状态寄存器（MXCSR）。<strong>不过只能在64位的模式下才能使用额外8个寄存器。</strong></p>
<p>•每个寄存器可以容纳4个32位单精度浮点数，或是2个64位双精度浮点数，或是4个32位整数，或是8个16位短整数，或是16个字符。整数运算能够使用正负号运算。而整数SIMD运算可能仍然要与8个64位MMX寄存器一起运行。</p>
<p>•因为操作系统必须要在进程切换的时候保护这些128位的寄存器状态，除非操作系统去引导这些寄存器，否则默认值是不会去激活的。这表示操作系统必须要知道如何使用<strong>FXSAVE与FXRSTOR</strong>指令才能存储X87与SSE寄存器的状态。</p>
<p><img src="https://i.loli.net/2021/09/17/YOFzj4lMnSBWg9d.png" alt="image-20210917115633701"></p>
<h3 id="SSE加入的新指令"><a href="#SSE加入的新指令" class="headerlink" title="SSE加入的新指令"></a>SSE加入的新指令</h3><p><strong>存储器到寄存器／寄存器到存储器／寄存器之间的资料搬移</strong></p>
<p>标量– MOVSS</p>
<p>包裹式– MOVAPS, MOVUPS, MOVLPS, MOVHPS, MOVLHPS, MOVHLPS</p>
<p><strong>数学运算</strong></p>
<p>标量– ADDSS, SUBSS, MULSS, DIVSS, RCPSS, SQRTSS, MAXSS, MINSS, RSQRTSS</p>
<p>包裹式– ADDPS, SUBPS, MULPS, DIVPS, RCPPS, SQRTPS, MAXPS, MINPS, RSQRTPS</p>
<p><strong>比较</strong></p>
<p>标量– CMPSS, COMISS, UCOMISS</p>
<p>包裹式– CMPPS</p>
<p>等等其他指令。</p>
<h3 id="关于包裹式"><a href="#关于包裹式" class="headerlink" title="关于包裹式"></a>关于包裹式</h3><p>•<strong>数据结构对齐</strong>是代码编译后在内存的布局与使用方式。包括三方面内容：<strong>数据对齐</strong>、<strong>数据结构填充</strong>（padding）与<strong>包入</strong>（packing）。</p>
<p>•现代计算机一般是32比特或64比特地址对齐，如果要访问的变量没有对齐，可能会触发总线错误。</p>
<p>•当数据小于计算机的字（word）尺寸，可能把几个数据元素放在一个字中，称为<strong>包入</strong>（packing）。</p>
<p><strong>简单来说包裹式意味着不止一个值被传输或作为操作数，而非打包则只处理一个值</strong>。</p>
<p>一个关于SSE的例子<br>$$<br>vec_res.x = v_1.x + v_2.x;\<br>vec_res.y = v_1.y + v_2.y;\<br>vec_res.z = v_1.z + v_2.z;\<br>vec_res.w = v_1.w + v_2.w;\<br>$$<br>左边这段代码会被编译成4条x86 FADD指令。下面的伪代码展示用128位包裹式相加（packed-add）指令替代4个纯量相加指令。</p>
<p><img src="https://i.loli.net/2021/09/17/oqL8ysYuCQErzPj.png" alt="image-20210917115843426"></p>

    </div>
    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/%E6%B1%87%E7%BC%96/" rel="tag"><i class="fa fa-tag"></i> 汇编</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/09/05/csapp-fgets/" rel="prev" title="fgets函数新版本tfgets">
      <i class="fa fa-chevron-left"></i> fgets函数新版本tfgets
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/09/17/%E5%8D%95%E7%89%87%E6%9C%BA%E6%9C%80%E7%BB%88%E5%AE%9E%E9%AA%8C/" rel="next" title="单片机最终实验设计与BSP开发">
      单片机最终实验设计与BSP开发 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B1%87%E7%BC%96%E6%B5%AE%E7%82%B9%E6%95%B0%E5%8E%9F%E7%90%86%E4%B8%8E%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.</span> <span class="nav-text">汇编浮点数原理与实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%91%E7%8E%B0%E4%B8%8D%E5%90%8C"><span class="nav-number">1.1.</span> <span class="nav-text">发现不同</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E7%9A%84%E5%AD%98%E5%82%A8"><span class="nav-number">1.1.1.</span> <span class="nav-text">浮点数的存储</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90"><span class="nav-number">1.1.2.</span> <span class="nav-text">一个例子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E7%9A%84%E5%AD%98%E5%82%A8%E8%BF%90%E7%AE%97"><span class="nav-number">1.2.</span> <span class="nav-text">浮点数的存储运算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8A%B6%E6%80%81%E5%AF%84%E5%AD%98%E5%99%A8"><span class="nav-number">1.3.</span> <span class="nav-text">状态寄存器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A7%E5%88%B6%E5%AF%84%E5%AD%98%E5%99%A8%E4%B8%8E%E6%A0%87%E5%BF%97%E5%AF%84%E5%AD%98%E5%99%A8"><span class="nav-number">1.4.</span> <span class="nav-text">控制寄存器与标志寄存器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E8%AE%A1%E7%AE%97%E6%8C%87%E4%BB%A4"><span class="nav-number">1.5.</span> <span class="nav-text">浮点数计算指令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%80%E5%8D%95%E7%9A%84%E5%88%86%E8%BE%A8%E6%8A%80%E5%B7%A7"><span class="nav-number">1.6.</span> <span class="nav-text">简单的分辨技巧</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E7%9A%84%E8%BF%90%E7%AE%97%E6%93%8D%E4%BD%9C"><span class="nav-number">1.7.</span> <span class="nav-text">具体的运算操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E7%9A%84%E6%8C%87%E4%BB%A4"><span class="nav-number">1.8.</span> <span class="nav-text">其他的指令</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%B0%E7%9A%84%E4%B8%8D%E5%90%8C"><span class="nav-number">2.</span> <span class="nav-text">新的不同</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E6%98%AF%E5%8E%9F%E6%9D%A5%E7%9A%84%E6%8C%87%E4%BB%A4%EF%BC%9F"><span class="nav-number">2.1.</span> <span class="nav-text">为什么不是原来的指令？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B3%E4%BA%8ESSE"><span class="nav-number">2.1.1.</span> <span class="nav-text">关于SSE</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SSE%E5%8A%A0%E5%85%A5%E7%9A%84%E6%96%B0%E6%8C%87%E4%BB%A4"><span class="nav-number">2.1.2.</span> <span class="nav-text">SSE加入的新指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E%E5%8C%85%E8%A3%B9%E5%BC%8F"><span class="nav-number">2.1.3.</span> <span class="nav-text">关于包裹式</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Sekla"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Sekla</p>
  <div class="site-description" itemprop="description">Sekla</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ShenTiao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShenTiao" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:584892986@qq.com" title="E-Mail → mailto:584892986@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1416875904&auto=0&height=66"></iframe>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sekla</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">288k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:22</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":500},"mobile":{"show":false},"react":{"opacity":0.9},"log":false});</script></body>
</html>

<script type="text/javascript" src="/js/src/love.js"></script>
